package com.jumi.microservice.controller.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.entity.JmExtraDiscount;
import com.jumi.microservice.entity.JmExtraDiscountChangeLog;
import com.jumi.microservice.entity.vo.ExtraDiscountVo;
import com.jumi.microservice.service.IJmExtraDiscountChangeLogService;
import com.jumi.microservice.service.IJmExtraDiscountService;
import com.jumi.microservice.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author ssk
 * @create 20/11/10 16:05
 **/
@RestController
@RequestMapping("/api/extraDiscount")
@Api(tags = "C-优惠折扣接口")
public class JmExtraDiscountClientController {

    @Resource
    private IJmExtraDiscountService discountService;

    @Resource
    private IJmExtraDiscountChangeLogService discountChangeLogService;


    @GetMapping("/findDiscountByUser")
    @ApiOperation(value = "根据用户查询折扣余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "查询详情的月份"),
            @ApiImplicitParam(name = "current", value = "分页当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "分页大小", required = true)
    })
    @Transactional
    public ResponseResult<ExtraDiscountVo> findDiscountByUser(String month, Integer current, Integer size) {
        Integer uid = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        ExtraDiscountVo vo = new ExtraDiscountVo();
        String errorMsg = "jmExtraDiscount";
        try {
            JmExtraDiscount jmExtraDiscount = discountService.getDiscountByUid(uid);
            if (jmExtraDiscount == null)
                return ResponseResult.success(vo);
            vo.setLeftDiscount(jmExtraDiscount.getDiscount());
            errorMsg = "usedDiscount";
            BigDecimal usedDiscount = discountChangeLogService.getUsedDiscountByUid(uid);
            vo.setUsedDiscount(usedDiscount);
            errorMsg = "JmExtraDiscountChangeLog-list";
            TableDataInfo<JmExtraDiscountChangeLog> table = this.findDiscountDetailByUser(month, uid, current, size);
            List<JmExtraDiscountChangeLog> list = (List<JmExtraDiscountChangeLog>) table.getRows();
            vo.setDetails(list);
        } catch (Exception e) {
            throw new BaseException(500, errorMsg + "查询出错!");
        }
        return ResponseResult.success(vo);
    }

    @GetMapping("/findDiscountDetailByUser")
    @ApiOperation(value = "根据用户查询折扣余额详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "查询详情的月份"),
            @ApiImplicitParam(name = "current", value = "分页当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "分页大小", required = true)
    })
    public TableDataInfo<JmExtraDiscountChangeLog> findDiscountDetailByUser(String month, Integer uid, Integer current, Integer size) {
        uid = uid == null ? ServletUtils.getIntHeader(JwtAdminInfo.apiUserId) : uid;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", uid);
        queryWrapper.orderByDesc("id");
        queryWrapper.in("discount_type", "reduce", "refund_back");
        if (ObjectUtil.isNotEmpty(month)) {
            Date date = DateUtil.parse(month, "yyyy-MM");
            Long beginTime = DateUtil.date(date).getTime() / 1000;
            Date endDate = DateUtil.endOfMonth(date);
            Long endTime = DateUtil.date(endDate).getTime() / 1000;
            queryWrapper.between("add_time", beginTime, endTime);
        }
        Page page = discountChangeLogService.getPage(new Page(current, size), queryWrapper);
        TableDataInfo dataTableInfo = PageService.getDataTableInfo(page);
        return dataTableInfo;
    }


}
